# -*- coding: utf-8 -*-
from threading import Thread,Lock
from Queue import Queue
import time
from random import random
  
class Fetcher:
    def __init__(self,threads):
        self.lock = Lock() #线程锁
        self.q_req = Queue() #任务队列
        self.q_ans = Queue() #完成队列
        self.threads = threads
        for i in range(threads):
            t = Thread(target=self.thread_get)
            t.setDaemon(True)
            t.start()
        self.running = 0
  
    def __del__(self): #解构时需等待两个队列完成
        time.sleep(0.5)
        self.q_req.join()
        self.q_ans.join()
  
    def task_left(self):
        return self.q_req.qsize()+self.q_ans.qsize()+self.running
  
    def push(self,req):
        self.q_req.put(req)
  
    def pop(self):
        return self.q_ans.get()
  
    def thread_get(self):
        while True:
            req = self.q_req.get()
            with self.lock: #要保证该操作的原子性，进入critical area
                self.running += 1
            time.sleep(random())
            print 'processing',req
            self.q_ans.put(req)
            with self.lock:
                self.running -= 1
            self.q_req.task_done()
            time.sleep(0.1) # don't spam
            
            
tasks = range(100)
f = Fetcher(threads=10)
for task in tasks:
    f.push(task)
while f.task_left():
    print f.pop()